/**
 * 比赛步骤和作品的中间表
 *
 * 任务1
 * 根据比赛步骤的guid获取作品的数量
 *
 *
 * 任务2
 * 根据比赛步骤查询作品
 */
module.exports = class extends think.Model
{
    get relation()
    {
        return {
            works:{
                type:think.Model.HAS_ONE,
                key:'FK_WorksGuid',
                fKey:'PK_WorksGuid'
            },
            grade:{
                type:think.Model.HAS_MANY,
                key:'PK_WorksNoGuid',
                fKey:'FK_WorksNoGuid'
            }

        }
    }

    //设置主键
    get pk(){
        return 'PK_WorksNoGuid';
    }

    async getWorksCountByWorkStepsGuid(workStepsGuid){
        return await this.setRelation(false).where({FK_CompetitionSteps:workStepsGuid}).count('PK_WorksNoGuid');
    }

    /**
     * 根据比赛步骤查询作品
     * @param workStepsGuid
     * @returns {Promise.<*>}
     */
    async getWorksByStepGuid(workStepsGuid){

        return await this.setRelation(true).where({FK_CompetitionSteps:workStepsGuid}).select();
    }
}